var app=getApp()
var util=require("../../utils/util")
var userModel=require("../../model/userModel.js")
var uploadUtil = require("../../utils/uploadUtil.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      user:app.data.user
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  //保存用户资料
  bindsubmit(e){
    var value=e.detail.value
    var data={
      gender:value.gender,
      name:value.name,
      avatar:this.data.user.avatar
    }
    util.showLoading("加载中")
    userModel.updateUserInfo(data,res=>{
      wx.hideLoading()
      if(res.status==1){
        util.failToast(res.msg)
        return
      }
      app.data.user.gender=data.gender
      app.data.user.name = data.name
      util.successToast("保存成功")
      setTimeout(()=>{
        wx.navigateBack()
      },800)
    })
  },
  bindAvatar(){
    var that=this
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      success: res => {
        var path = res.tempFiles[0].path
        util.showLoading("加载中")
        uploadUtil.uploadImage(path, url => {
          wx.hideLoading()
          var user=that.data.user
          user.avatar=url
          app.data.user = user
          that.setData({
            user:user
          })
        })
      }
    })
  }
})